<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{
	var $has_error = array();

	public function set_rules($input, $rules)
	{
		$value = $this->CI->input->post($input, false);
		$this->check_input[] = array(
					"name" => $input,
					"value" => trim($value),
					"rules" => $rules
				);
	}

	public function run()
	{
		if(empty($this->check_input)) {
			return true;
		}
		$this->_error_array = array();
		foreach ($this->check_input as $input) {
			$rules = $input["rules"];
			if(!is_array($input["rules"])) {
				$rules = explode("|", $input["rules"]);
			}
			foreach($rules as $rule) {
				$param = FALSE;
				if (preg_match("/(.*?)\[(.*)\]/", $rule, $match)) {
					$rule = $match[1];
					$param = $match[2];
				}
				if (method_exists($this, $rule)) {
					$result = $this->$rule($input["value"], $param);
					if ($result === FALSE) {
						if(!isset($this->_error_array[$input["name"]])) {
							$this->_error_array[$input["name"]] = array();
						}
						$this->_error_array[$input["name"]][$rule] = $param;
					}
				}
			}
		}
		if(count($this->_error_array)) {
			return $this->_error_array;
		}
		return true;
	}
}